package com.ai.manager.edu.cms.device.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Component;

import com.ai.common.utils.pagination.PaginationUtil;
import com.ai.dao.edu.cms.device.UserDeviceMapper;
import com.ai.manager.edu.cms.device.UserDeviceManager;
import com.ai.model.edu.cms.device.UserDevice;
import com.ai.model.edu.cms.device.vo.UserDeviceVO;
import com.ai.model.edu.user.dto.UserDeviceDto;

@Component("userDeviceManager")
public class UserDeviceManagerImpl implements UserDeviceManager {

	private Log log = LogFactory.getLog(getClass());
	@Resource
	private UserDeviceMapper userDeviceMapper;
	
	@Resource
	private SqlSessionTemplate sqlSessionTemplate;
	@Override
	public void betchInsertUserDeviceInfo(List<UserDevice> list) {
		List<List<UserDevice>> split = PaginationUtil.split(list, 10);
		for(List<UserDevice> temp: split){
			userDeviceMapper.betchInsertUserDeviceInfo(temp);
		}
	}
	@Override
	public List<UserDeviceVO> findUserDeviceListByUserIds(Map<String,Object> map) {
		return userDeviceMapper.findUserDeviceListByUserIds(map);
	}
	@Override
	public List<UserDeviceVO> findUsersByInfoObjs(Map<String, Object> map) {
		
		return userDeviceMapper.findUsersByInfoObjs(map);
	}
	@Override
	public void betchUpdateUserDeviceInfo(List<UserDevice> list) {
		
		List<List<UserDevice>> split = PaginationUtil.split(list, 500);
		//批量
		SqlSession session = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH);
		try {
			UserDeviceMapper mapper = session.getMapper(UserDeviceMapper.class);
			for(List<UserDevice> tmp : split){
				mapper.betchUpdateUserDeviceInfo(tmp);
				session.commit();
				session.clearCache();
			}
		} catch (Exception e) {
			log.error("批量更新异常" , e);
			session.rollback();
		} finally{
			session.close();
		}
	}
    @Override
    public List<UserDeviceVO> queryUserDeviceInfoWithPgByDto(UserDeviceDto userDeviceDto) {
        return userDeviceMapper.queryUserDeviceInfoWithPgByDto(userDeviceDto);
    }
    @Override
    public Integer countQueryUserDeviceInfoWithPgByDto(UserDeviceDto userDeviceDto) {
        return userDeviceMapper.countQueryUserDeviceInfoWithPgByDto(userDeviceDto);
    }

}
